﻿<%@ page import="java.util.HashMap,
                 com.ptc.netmarkets.util.misc.NmAction,
                 com.ptc.netmarkets.util.misc.NmActionServiceHelper,
                 com.ptc.netmarkets.work.NmWorkItem ,
				 com.ptc.netmarkets.work.NmWorkItemCommands,
				 com.ptc.netmarkets.model.NmNamedObject,
				 wt.workflow.worklist.worklistResource,
				 wt.util.WTMessage,
				 java.util.Locale,
				 java.util.ResourceBundle,
				 com.ptc.netmarkets.util.misc.NmHTMLActionModel,
				 com.ptc.netmarkets.util.misc.NmContext,wt.session.SessionHelper,
				 ext.acae2.workflow.TaskConfigrationHelper" 
%>

<jsp:useBean id="modelBean" class="com.ptc.netmarkets.util.beans.NmModelBean" scope="request"/>
<jsp:useBean id="localeBean" class="com.ptc.netmarkets.util.beans.NmLocaleBean" scope="request"/>
<jsp:useBean id="nmcontext" class="com.ptc.netmarkets.util.beans.NmContextBean" scope="request"/>
<jsp:useBean id="urlFactoryBean" class="com.ptc.netmarkets.util.beans.NmURLFactoryBean"  scope="request"/>
<jsp:useBean id="commandBean" class="com.ptc.netmarkets.util.beans.NmCommandBean" scope="request"/>
<jsp:useBean id="actionBean" class="com.ptc.netmarkets.util.beans.NmActionBean" scope="request"/>
<jsp:useBean id="objectBean" class="com.ptc.netmarkets.util.beans.NmObjectBean" scope="request"/>
<jsp:useBean id="checkBoxBean" class="com.ptc.netmarkets.util.beans.NmCheckBoxBean"  scope="request"/>
<jsp:useBean id="textBoxBean"  class="com.ptc.netmarkets.util.beans.NmTextBoxBean"  scope="request"/>
<jsp:useBean id="radioButtonBean" class="com.ptc.netmarkets.util.beans.NmRadioButtonBean"  scope="request"/>
<jsp:useBean id="textAreaBean" class="com.ptc.netmarkets.util.beans.NmTextAreaBean"  scope="request"/>
<jsp:useBean id="comboBoxBean" class="com.ptc.netmarkets.util.beans.NmComboBoxBean"  scope="request"/>
<jsp:useBean id="dateBean"    class="com.ptc.netmarkets.util.beans.NmDateBean"    scope="request"/>
<jsp:useBean id="stringBean" class="com.ptc.netmarkets.util.beans.NmStringBean"  scope="request"/>
<jsp:useBean id="linkBean"    class="com.ptc.netmarkets.util.beans.NmLinkBean"    scope="request"/>
<jsp:useBean id="sessionBean" class="com.ptc.netmarkets.util.beans.NmSessionBean" scope="request"/>	

<%!
    private static final String RESOURCE = "wt.workflow.worklist.worklistResource";
	private static final String WORK_RESOURCE = "com.ptc.netmarkets.work.workResource";
	Boolean isShow =Boolean.TRUE;
	//String wioid = request.getParameter("oid");
%>

<%
     ResourceBundle WorklistResourceRb = ResourceBundle.getBundle(RESOURCE, localeBean.getLocale());
%>
<SCRIPT LANGUAGE="JavaScript">


function acae_toggle_select_all(){
	var allcheck = document.getElementById('acae_dept_select_all');
	if(allcheck==null)
		return ;
	var section = document.getElementById('acae_department_selection');
	if(section==null)
		return ;
	var selection = section.getElementsByTagName('input');
	for(var k=0;k<selection.length;k++){
		if(selection[k].type='checkbox')
			selection[k].checked=allcheck.checked;
	}
}

function listenEnterOnPassword(evt) {
	 if (!evt) evt = window.event; // for IE
	 if (evt.keyCode) code = evt.keyCode
	 if (evt.which) code = evt.which
	
	 if(code != Event.KEY_RETURN) {
	 	return;
	 }
	 clickCompleteButton();
	 Event.stop(evt);
	 return false;
}

function clickCompleteButton() {
	 if(document.getElementsByName("complete")) {
		 var completeButton = document.getElementsByName("complete")[0];
		 completeButton.click();
	 }
} 

function validateAdhocAssignee() {
    var shouldSubmitResponse = false;
	var pathname = window.location.pathname;
    var splitPathname = pathname.split("/");
    var wcHome = window.location.protocol + "//" + window.location.host + "/" + splitPathname[1] + "/";
    var jspName = 'netmarkets/jsp/workitem/validateAdhocAssignee.jsp';
    var fullURL = wcHome + jspName;

    // want to get no. of elements from the tbody rows
    var tbody = document.getElementById('tb__Adhoc_Act_Id');
    var noOfAssignee = tbody.getElementsByTagName('tr').length;
    var selection = '';
	var adhocAssigneeCount = 0;
	var adhocAssigneeNameCount = 0;

	for (var i = 0; i < noOfAssignee; i++)
    {
        var selectedAssignee = document.getElementsByName("___AD_HOC_ASSIGNEE" + i +"___textbox")[0].value;
		var selectedActvityName = document.getElementsByName("___AD_HOC_ACTIVITY_NAME" + i +"___textbox")[0].value;

		if(selectedActvityName.length > 0){
				adhocAssigneeNameCount++;
		}

        if (selectedAssignee.length > 0) {
        	adhocAssigneeCount++;
            selection += selectedAssignee;
			
			if(selection.length > 0 && i < noOfAssignee)
				selection += ',';
        }
    }
	//alert('Name :: '+adhocAssigneeNameCount+'----Assignee :: '+adhocAssigneeCount)
    if(adhocAssigneeNameCount == 0 || adhocAssigneeCount ==0){
		JCAAlert('wt.workflow.worklist.worklistResource.ADHOC_VALIDATION_ERROR1');
		return shouldSubmitResponse;
	}
	if(adhocAssigneeNameCount != adhocAssigneeCount){
		JCAAlert('wt.workflow.worklist.worklistResource.ADHOC_VALIDATION_ERROR2');
		return shouldSubmitResponse;
	}
	if(adhocAssigneeCount > 0)
    {
    	selection = selection.substring(0, selection.length-1);
    }	

	var loader = new ajax.ContentLoader(fullURL, null, null, 'POST', '&adhocAssignees='+selection+'&oid='+getParamFromQueryString('oid'));
    var resText = null;
	if (loader.req.readyState == 4 && loader.req.status == 200) {
        resText = loader.req.responseText;
		
		if(resText == null || resText.length <= 0){
			shouldSubmitResponse = true;
		}
		else {
			alert(resText); 
		}
    }
    return shouldSubmitResponse;
}

function getParamFromQueryString(paramToGet) {
	var hostUrl = window.location.search.substring(1);
	var paramValue = new Array();
	var paramValuePairs = hostUrl.split("&");

	for (i=0;i<paramValuePairs.length;i++) {
		paramValue = paramValuePairs[i].split("=");
		if (paramValue[0] == paramToGet) {
			var valueToReturn = paramValue[1];
			valueToReturn = valueToReturn.replace(/%3A/g, ':');
			return valueToReturn;
		}
	}
}
function EnableTextBox(){
	var inputVar = $$('input');
	for(var i=0; i<inputVar.length; i++){
            if (inputVar[i].disabled && inputVar[i].type == 'text') {
                inputVar[i].disabled = false;
                inputVar[i].readonly = false;
            }
	}
	//return PTC.wizard.checkRequired(false,false); //OOTB
	return true;//acae 
}

function clearUserPickerTextBox(name)
{
    var ele = document.forms[0].elements;
    for(var i=0;i<ele.length;i++)
    {
        if((ele[i].name === name)&&(ele[i].type === "button"))
        {
            ele[i-3].value = "";    
        }
    }

}

</SCRIPT>
	           
<%
  nmcontext.setRequest(request);
  nmcontext.setResponse(response);
  nmcontext.adjustContext(request);
  com.ptc.netmarkets.util.beans.NmCommandBean cb = new com.ptc.netmarkets.util.beans.NmCommandBean();
  cb.setCompContext(nmcontext.getContext().toString());
  cb.setRequest(request);
  cb.setSessionBean(sessionBean);
  NmWorkItem myNmWorkItem = (NmWorkItem) NmWorkItemCommands.view(cb);

	String curUser = SessionHelper.getPrincipal().getName();					
	Boolean disAllowFlag = Boolean.FALSE;
	Boolean isPM =Boolean.FALSE;
	HashMap additionInfo = myNmWorkItem.getAdditionalInfo();
	if(additionInfo!=null&&additionInfo.containsKey("DisallowFlag")&&additionInfo.containsKey("ProjectMangerFlag"))
	{
		disAllowFlag = (Boolean)additionInfo.get("DisallowFlag");
		isPM = (Boolean)additionInfo.get("ProjectMangerFlag");
		if(disAllowFlag.equals(Boolean.TRUE))
		{  
			String workItemOwnerName = null;
			if(additionInfo.containsKey("CurrentOwner"))
				workItemOwnerName = (String)additionInfo.get("CurrentOwner");
			if(workItemOwnerName==null || workItemOwnerName.equals(curUser))
				disAllowFlag = Boolean.FALSE;
		
			if(isPM.equals(Boolean.TRUE))
			{
				if(disAllowFlag.equals(Boolean.FALSE))
					isShow=Boolean.TRUE;
				else
					isShow=Boolean.FALSE;
			}
			else
			{
				if(disAllowFlag.equals(Boolean.FALSE))
					isShow=Boolean.TRUE;
			}
		}

	}

  %>      

  

<div id="completButtonId">               
<table class="pp" cellspacing="1" cellpadding="1" height="5">
<tr  class="basefont" align="right" height="5">
<td align="center" height="5">
<%
/**add by cnie for acae2 begin*/
String wioid = request.getParameter("oid");
Object obj = TaskConfigrationHelper.getActivityVariableByVarName(wioid,"isImported");
/**add by cnie for acae2 end*/
Locale locale = localeBean.getLocale();
response.setCharacterEncoding("UTF-8");
	NmHTMLActionModel am = null;
	 String show= request.getParameter("showAdhocComponent");
	 
	 

if(!myNmWorkItem.isAdhocInProgress())
{
	
if (myNmWorkItem.isProjectWorkItem() && !myNmWorkItem.isAdhocActivity()) {
		am = NmActionServiceHelper.service.getActionModel("update workitem",myNmWorkItem);
		NmAction updateTask = (NmAction)am.getActions().get(0);
		updateTask.setButton(true);
		updateTask.setIcon(null);
		updateTask.setEnabled((!myNmWorkItem.isCompleted() && !myNmWorkItem.isSuspended() && myNmWorkItem.isMine()));
		//boolean adminTrackCostOverride=;
		//if (adminTrackCostOverride) updateTask.setEnabled(true);
		actionBean.setAction(updateTask);
		NmContext context = nmcontext.getContext();
		context.pushElement(myNmWorkItem);
		NmAction.actionjsp( actionBean,  linkBean,  objectBean,  localeBean,  urlFactoryBean,nmcontext,  sessionBean,  out,  request,  response);
		context.popElement();
		actionBean.setAction(null);
	}else {
		
		if(isShow.equals(Boolean.TRUE))
		{
		am = NmActionServiceHelper.service.getActionModel("save workitem",myNmWorkItem);
		NmAction saveTask = (NmAction)am.getActions().get(0);
		saveTask.setButton(true);
		saveTask.setEnabled((!myNmWorkItem.isCompleted() && !myNmWorkItem.isSuspended() && myNmWorkItem.isMine()));
		actionBean.setAction(saveTask);
		NmContext context2 = nmcontext.getContext();
		context2.pushElement(myNmWorkItem);
		NmAction.actionjsp( actionBean,  linkBean,  objectBean,  localeBean,  urlFactoryBean,nmcontext,  sessionBean,  out,  request,  response);
		context2.popElement();
		actionBean.setAction(null);
	}
}
}
%>
</td>
<td align="center" height="5">
<%

	if(!myNmWorkItem.isAdhocActivity() || !(show !=null && show.equals("table")) )
	{
		if(myNmWorkItem.isAdhocActivity() && myNmWorkItem.isAdhocInProgress() )
		{
		
  				    out.println (new WTMessage (RESOURCE, worklistResource.AD_HOC_IN_PROGRESS, null).getLocalizedMessage (locale));
					out.println("<br></br>");
		}
		else 
		{
			if(isShow.equals(Boolean.TRUE))
			{
			am = NmActionServiceHelper.service.getActionModel("complete workitem",myNmWorkItem);
			 NmAction completeTask = (NmAction)am.getActions().get(0);
			completeTask.setButton(true);
			//completeTask.setEnabled(true);
			completeTask.setEnabled((!myNmWorkItem.isCompleted() && !myNmWorkItem.isSuspended() && myNmWorkItem.isMine()));
			actionBean.setAction(completeTask);
			NmContext context = nmcontext.getContext();
			context.pushElement(myNmWorkItem);
			NmAction.actionjsp( actionBean,  linkBean,  objectBean,  localeBean,  urlFactoryBean,nmcontext,  sessionBean,  out,  request,  response);
			context.popElement();
			actionBean.setAction(null);
		}
	}
	}
	else
	{
		
		am = NmActionServiceHelper.service.getActionModel("start Activities",myNmWorkItem);
		NmAction startTask = (NmAction)am.getActions().get(0);
		startTask.setButton(true);
		startTask.setEnabled((!myNmWorkItem.isCompleted() && !myNmWorkItem.isSuspended() && myNmWorkItem.isMine()));
		actionBean.setAction(startTask);
		NmContext context1 = nmcontext.getContext();
		context1.pushElement(myNmWorkItem);
		NmAction.actionjsp( actionBean,  linkBean,  objectBean,  localeBean,  urlFactoryBean,nmcontext,  sessionBean,  out,  request,  response);
		context1.popElement();
		actionBean.setAction(null);
	}
	 
%>
 <input type="hidden" name="completehidden" id="completehidden" onclick="" />
 <input type="hidden" id="flagBtn" value=""/>
 <script type="text/javascript">

function getNetObject(number) {
    if (number == 1) {
        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            netObject1 = new XMLHttpRequest();
            if (netObject1.overrideMimeType) {
                netObject1.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject) { // IE
            try {
                netObject1 = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    netObject1 = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                    alert("your browse doesn't suport ajax");
                }
            }
        }
        return netObject1;
    } else if (number == 2) {
        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            netObject2 = new XMLHttpRequest();
            if (netObject2.overrideMimeType) {
                netObject2.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject) { // IE
            try {
                netObject2 = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    netObject2 = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                    alert("your browse doesn't suport ajax");
                }
            }
        }

        return netObject2;
    } else {

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            netObject3 = new XMLHttpRequest();
            if (netObject3.overrideMimeType) {
                netObject3.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject) { // IE
            try {
                netObject3 = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    netObject3 = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                    alert("your browse doesn't suport ajax");
                }
            }
        }

        return netObject3;
    }
}

function replaceTaskCompleteButton() {
        
		var completeBtn = document.getElementsByName("complete")[0];
		var completeBtn1 = document.getElementsByName("complete")[1];
    	var completehiddenBtn = document.getElementsByName("completehidden")[0];
    	if (completeBtn&&completehiddenBtn) { 
		  var flagBtn = document.getElementById("flagBtn");
		  if(!flagBtn.value){
		    completehiddenBtn.onclick = completeBtn.onclick;
			completeBtn.onclick = validata;  
            completeBtn1.onclick = validata;
			flagBtn.value="one";
		  }	  
       }
}    

function replaceTaskCompleteButton2() {
    
	var completeBtn = document.getElementsByName("complete")[0];
	var completeBtn1 = document.getElementsByName("complete")[1];
	var completehiddenBtn = document.getElementsByName("completehidden")[0];
	if (completeBtn&&completehiddenBtn) { 
	  var flagBtn = document.getElementById("flagBtn");
	  if(!flagBtn.value){
	    completehiddenBtn.onclick = completeBtn.onclick;
		completeBtn.onclick = checkSelectDept;  
        completeBtn1.onclick = checkSelectDept;
		flagBtn.value="one";
	  }	  
   }
}  

function checkSelectDept(){
	var completehiddenBtn = document.getElementsByName("completehidden")[0];
	var sendDeptArr = document.getElementsByName("sendDept");
    for(var i=0;i<sendDeptArr.length;i++){
	   var sendDeptObj = sendDeptArr[i];
	   if(sendDeptObj.checked){
		   completehiddenBtn.onclick();
		   return;
	   }
	}
    alert("请选择发放单位");
}

function validata(){
    var completehiddenBtn = document.getElementsByName("completehidden")[0];
	var routingA = document.getElementById("routingChoice_流程导入");
	if(routingA.checked){
	   var netObject = getNetObject(1);
       netObject.onreadystatechange = function() {
        if (netObject.readyState == 4) {
            if (netObject.status == 200) {
                myjson = netObject.responseText;	
            } else {
                alert('There was a problem with the request.');
            }
        }
      }
      netObject.open("GET", "netmarkets/jsp/ext/acae2/dms/checkImport.jsp?oid=<%=wioid%>", false);
      netObject.setRequestHeader("If-Modified-Since", "0");
      netObject.send(null);
	  if(myjson.trim()=="true"){
		 completehiddenBtn.onclick();
	  }else{
		 alert("请正确完成导入功能");
	  }
	}else{
	   completehiddenBtn.onclick();
	} 
}  	

var objFlag = <%=obj%>;
//alert(typeof objFlag);
//alert(objFlag==null);
if(objFlag!==null){
  // replaceTaskCompleteButton();
}

var sendDeptArr = document.getElementsByName("sendDept");
if(sendDeptArr.length>0){
	replaceTaskCompleteButton2();
}


function selectDepartment(obj){
    var selDepartment="";
    var sendDeptArr = document.getElementsByName("sendDept");
    for(var i=0;i<sendDeptArr.length;i++){
	   var sendDeptObj = sendDeptArr[i];
	   if(sendDeptObj.checked){
	       var value = sendDeptObj.value;
	       selDepartment=selDepartment+value+",";
	   }
	}
	var url;
	if(selDepartment){
	  url= "netmarkets/jsp/ext/acae2/dms/setSelectedDept.jsp?oid=<%=wioid%>&selDepartment="+selDepartment;
	}else{
	  url= "netmarkets/jsp/ext/acae2/dms/setSelectedDept.jsp?oid=<%=wioid%>";
	}
	var netObject = getNetObject(1);
    netObject.onreadystatechange = function() {
        if (netObject.readyState == 4) {
            if (netObject.status == 200) {
                myjson = netObject.responseText;	
            } else {
                alert('There was a problem with the request.');
            }
        }
    }
	
	
	//alert(selDepartment);
    netObject.open("GET", url, false);
    netObject.setRequestHeader("If-Modified-Since", "0");
    netObject.send(null);

}


function selectALLDept(obj){
    var selDepartment="";
    //alert(obj.checked);
	var sendDeptArr = document.getElementsByName("sendDept");
	var url;
    if(obj.checked){
	   for(var i=0;i<sendDeptArr.length;i++){
	      var sendDeptObj = sendDeptArr[i];
	      var value = sendDeptObj.value;
	      //alert(sendDeptObj.checked);
	      if(!sendDeptObj.checked){
		    sendDeptObj.checked="checked";
	      }
		  selDepartment=selDepartment+value+",";
	    }
		url= "netmarkets/jsp/ext/acae2/dms/setSelectedDept.jsp?oid=<%=wioid%>&selDepartment="+selDepartment;
	}else{
	    for(var i=0;i<sendDeptArr.length;i++){
		   var sendDeptObj = sendDeptArr[i];
		   if(sendDeptObj.checked){
		      sendDeptObj.checked="";
		   }
		}
		url= "netmarkets/jsp/ext/acae2/dms/setSelectedDept.jsp?oid=<%=wioid%>";
	}
    var netObject = getNetObject(1);
    netObject.onreadystatechange = function() {
        if (netObject.readyState == 4) {
            if (netObject.status == 200) {
                myjson = netObject.responseText;	
            } else {
                alert('There was a problem with the request.');
            }
        }
    }
	//alert(selDepartment);
    netObject.open("GET", url, false);
    netObject.setRequestHeader("If-Modified-Since", "0");
    netObject.send(null);
}

function setBaselineState(){
	var url= "netmarkets/jsp/ext/acae2/baseline/setBaselineState.jsp?oid=<%=wioid%>";
	var netObject = getNetObject(1);
    netObject.onreadystatechange = function() {
        if (netObject.readyState == 4) {
            if (netObject.status == 200) {
                myjson = netObject.responseText;
                alert(myjson.trim());
            } else {
                alert('There was a problem with the request.');
            }
        }
    }
	
	
	//alert(selDepartment);
    netObject.open("GET", url, false);
    netObject.setRequestHeader("If-Modified-Since", "0");
    netObject.send(null);
}

 </script>
</td>
</table>
</div>
<!-- OOTB -->
<!--input type="hidden" id="requiredMessage" name="requiredMessage" value="<%=WTMessage.getLocalizedMessage(com.ptc.core.ui.errorMessagesRB.class.getName(),com.ptc.core.ui.errorMessagesRB.CREATE_WITHOUT_REQUIRED_FIELDS, null, locale)%>"--> 
